package com.simple.datax.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.simple.base.controller.AbstractBaseController;
import com.simple.util.TbResourceLoad;
import com.simple.util.common.Constants;
import com.simple.util.object.JFValidationBean;
import com.simple.util.object.PageInfoBean;
import com.simple.util.object.datax.DataxUserBean;




public abstract class AbstractDataxBaseController extends AbstractBaseController{
	
	
	public DataxUserBean getCurrentDataxUserBean(final HttpServletRequest request){
		return (DataxUserBean) request.getSession().getAttribute(Constants.CURRENT_SESSION_USERBEAN);
	}
		
	
	public void isHaveRight(final String functionId, final HttpServletRequest request){
		final DataxUserBean userBean = (DataxUserBean) request.getSession().getAttribute("currentUserBean");
		final List<JFValidationBean> errorList = getErrorList();
		if(userBean == null){
			errorList.add(new JFValidationBean("message.login.userbean.null"));
		}
	}
	
	protected String generatePageInfo(final HttpServletRequest request, final HttpServletResponse reponse, final PageInfoBean pageInfoBean, final String goPageFunctionNname) {
        String sPageInfo = "";
        final StringBuffer pageInfo = new StringBuffer(500);
        if(pageInfoBean.getTotalPageNo()>1){
            final String locale = getSiteLanguage(request);
            if(pageInfoBean.getCurPageNo() >1){
                pageInfo.append("<a title='First Page' href='javascript:" + goPageFunctionNname+"(1)'>"+ TbResourceLoad.getInstance().getString(Constants.TBRESOURCES_PACKAGE_WEB, locale, "web.system.pageinfo.first")+"</a>");
            }
            
            if(pageInfoBean.getCurPageNo() >1){
                pageInfo.append("<a title='Previous Page' href='javascript:" + goPageFunctionNname+"("+(pageInfoBean.getCurPageNo()-1)+")'>"+ TbResourceLoad.getInstance().getString(Constants.TBRESOURCES_PACKAGE_WEB, locale, "web.system.pageinfo.prior")+"</a>");
            }
            
            
            int beginNo =  pageInfoBean.getCurPageNo() - (int)Math.ceil((double)PageInfoBean.COMMON_PAGE_COUNT_IN_ONE_TIME/2);
            if(beginNo<1){
                beginNo = 1;
            }
            
            int endNo = pageInfoBean.getCurPageNo() + (PageInfoBean.COMMON_PAGE_COUNT_IN_ONE_TIME-(int)Math.ceil((double)PageInfoBean.COMMON_PAGE_COUNT_IN_ONE_TIME/2)-1);
            if(endNo>pageInfoBean.getTotalPageNo()){
                endNo = pageInfoBean.getTotalPageNo();
            }
            
            int nShowPageCount = endNo - beginNo + 1;
            if(nShowPageCount<PageInfoBean.COMMON_PAGE_COUNT_IN_ONE_TIME){
                beginNo = beginNo - (PageInfoBean.COMMON_PAGE_COUNT_IN_ONE_TIME-nShowPageCount);
                if(beginNo<1){
                    beginNo = 1;
                }
            }
            
            nShowPageCount = endNo - beginNo + 1;
            if(nShowPageCount<PageInfoBean.COMMON_PAGE_COUNT_IN_ONE_TIME){
                endNo = endNo + (PageInfoBean.COMMON_PAGE_COUNT_IN_ONE_TIME-nShowPageCount);
                if(endNo>pageInfoBean.getTotalPageNo()){
                    endNo = pageInfoBean.getTotalPageNo();
                }
            }
            
            for(int i=beginNo;i<=endNo;i++){
                if(i==pageInfoBean.getCurPageNo()){
                    pageInfo.append("<a title='"+i+"' class='number current'>"+i+"</a>");
                }
                else{
                    pageInfo.append("<a title='"+i+"' class='number' href='javascript:" + goPageFunctionNname+"("+i+")'>"+i+"</a>");
                }
            }
            
            if(pageInfoBean.getCurPageNo() < pageInfoBean.getTotalPageNo()){
                pageInfo.append("<a title='Next Page' href='javascript:" + goPageFunctionNname+"("+(pageInfoBean.getCurPageNo()+1)+")'>"+ TbResourceLoad.getInstance().getString(Constants.TBRESOURCES_PACKAGE_WEB, locale, "web.system.pageinfo.next")+"</a>");
            }
            
            if(pageInfoBean.getCurPageNo() < pageInfoBean.getTotalPageNo()){
                pageInfo.append("<a title='Last Page' href='javascript:" + goPageFunctionNname+"("+pageInfoBean.getTotalPageNo()+")'>"+ TbResourceLoad.getInstance().getString(Constants.TBRESOURCES_PACKAGE_WEB, locale, "web.system.pageinfo.last")+"</a>");
            }
            
            
            sPageInfo = "<div class='pagination'>"+pageInfo.toString()+"</div>";
        }
        return sPageInfo;
    }
	
}
